package com.zktr.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.zktr.dao.OrderInfoDao;
import com.zktr.pojo.OrderInfo;
import com.zktr.pojo.OrderInfoBean;
import com.zktr.pojo.PageBean;
/**
 * 订单详情表
 * @author 123
 *
 */
public class OrderinfoService {
	private OrderInfoDao dao=new OrderInfoDao();
	/**
	 * 测试通过
	 * @param args
	 */
	public static void main(String[] args) {
		/*OrderInfo info=new OrderInfo<>();
		info.setOrderid("1");
		info.setConsignne("李四");
		info.setOrderstatus("0");
		OrderinfoService service=new OrderinfoService();
		PageBean<OrderInfo> findOrders = service.findOrders(info,2, 3);
		PageBean<OrderInfoBean> findAllOrders = service.findAllOrders(1, 3);
		 List<OrderInfoBean> beanList = findAllOrders.getBeanList();
		for (OrderInfoBean orderInfoBean : beanList) {
			System.out.println(orderInfoBean);
		}*/
		/*OrderInfo info=new OrderInfo<>();
		info.setOrderid(1);
		OrderinfoService service=new OrderinfoService();
		OrderInfoBean findOneOrder = service.findOneOrder(info);
		System.out.println(findOneOrder);
		*/
	}
	/**
	 * 订单列表：分页查询全部
	 * @return 
	 */
	public PageBean<OrderInfoBean>  findAllOrders(int pc,int ps) {
		Map<Integer, List<OrderInfo>> selectBooks = dao.selectOrders(pc, ps);
		List<OrderInfoBean> list2=new LinkedList<>();
		status(selectBooks.get(0), list2);
		PageBean<OrderInfoBean> bean=new PageBean<OrderInfoBean>();
		bean.setPc(pc);//页码
		bean.setPs(ps);//页大小
		bean.setBeanList(list2);
		bean.setTr(dao.selectOrderinfoCount());//总记录条数
		return bean;
	}
	/**
	 * 查询订单：按条件分页查询
	 * @param info
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<OrderInfoBean> findOrders(OrderInfo info, int pc, int ps) {
		Map<Integer, List<OrderInfo>> selectBooks = dao.selectOrders(info,pc, ps);
		List<OrderInfoBean> list2=new LinkedList<>();
		PageBean<OrderInfoBean> bean=new PageBean<OrderInfoBean>();
		status(selectBooks.get(0), list2);
		bean.setPc(pc);//页码
		bean.setPs(ps);//页大小
		bean.setBeanList(list2);
		bean.setTr(dao.selectOrderinfoCount(info));//总记录条数
		return bean;
	}
	/**
	 * 修改订单操作：
	 * @param info
	 * @return 
	 */
	public int updateorder(OrderInfo info,String id) {
		return dao.updateorderinfo(info,id);
	}
	/**
	 * 详情操作：
	 * @param info
	 * @return
	 */
	public OrderInfoBean findOneOrder(OrderInfo info) {
		// TODO Auto-generated method stub
		List<OrderInfo> list = dao.selectOneOrder(info);
		List<OrderInfoBean> list2=new LinkedList<>();
		status(list, list2);
		return list2.get(0);
	}
	/**
	 * 退货单
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<OrderInfoBean> findsendOrders(int pc, int ps) {
		Map<Integer, List<OrderInfo>> selectBooks = dao.selectsendOrders(pc, ps);
		List<OrderInfoBean> list2=new LinkedList<>();
		status(selectBooks.get(0), list2);
		PageBean<OrderInfoBean> bean=new PageBean<OrderInfoBean>();
		bean.setPc(pc);//页码
		bean.setPs(ps);//页大小
		bean.setBeanList(list2);
		bean.setTr(dao.selectsendCount());//总记录条数
		return bean;
	}
	/**
	 * 数据类型转换
	 * @param list
	 * @param list2
	 */
	private void status(List<OrderInfo> list, List<OrderInfoBean> list2) {
		for (OrderInfo orderInfo : list) {
			
			OrderInfoBean oBean=new OrderInfoBean();
			oBean.setAddress(orderInfo.getAddress());
			oBean.setAddtime(mydate(orderInfo.getAddtime()));
			oBean.setConfirmtime(mydate(orderInfo.getConfirmtime()));
			oBean.setConsignne(orderInfo.getConsignne());
			oBean.setGoodsamount(orderInfo.getGoodsamount()+"");
			oBean.setIntegral(orderInfo.getIntegral()+"");
			oBean.setIntegralmoney(orderInfo.getIntegralmoney()+"");
			oBean.setMobile(orderInfo.getMobile());
			oBean.setOrderamount(orderInfo.getOrderamount()+"");
			oBean.setOrderid(orderInfo.getOrderid()+"");
			oBean.setOrdersn(orderInfo.getOrdersn());
			oBean.setParentid(orderInfo.getParentid()+"");
			oBean.setPaynote(orderInfo.getPaynote());
			oBean.setPaytime(mydate(orderInfo.getPaytime()));
			oBean.setPostscript(orderInfo.getPostscript());
			oBean.setShippingfee(orderInfo.getShippingfee()+"");
			oBean.setShippingid(orderInfo.getShippingid()+"");
			oBean.setShippingtime(mydate(orderInfo.getShippingtime()));
			oBean.setUserid(orderInfo.getUserid()+"");
				if (orderInfo.getOrderstatus()==0){
					oBean.setOrderstatus("未确认");
				} else if(orderInfo.getOrderstatus()==1) {
					oBean.setOrderstatus("确认");
				}else if(orderInfo.getOrderstatus()==2) {
					oBean.setOrderstatus("已取消");
				}else if(orderInfo.getOrderstatus()==3) {
					oBean.setOrderstatus("无效");
				}else if(orderInfo.getOrderstatus()==4) {
					oBean.setOrderstatus("退货");
				}
				if(orderInfo.getShippingstatus()==0) {
					oBean.setShippingstatus("未发货");
				}else if(orderInfo.getShippingstatus()==1) {
					oBean.setShippingstatus("已发货");
				}else if(orderInfo.getShippingstatus()==2) {
					oBean.setShippingstatus("已收货");
				}else if(orderInfo.getShippingstatus()==4) {
					oBean.setShippingstatus("退货");
				}
				if(orderInfo.getPaystatus()==0) {
					oBean.setPaystatus("未付款");
				}else if(orderInfo.getPaystatus()==1) {
					oBean.setPaystatus("付款中");
				}else if(orderInfo.getPaystatus()==2) {
					oBean.setPaystatus("已付款");
				}
				list2.add(oBean);
				System.out.println(oBean);
		}
	}
	/**
	 * 日期转换：
	 * @param addtime
	 * @return
	 */
	private String mydate(int addtime) {
		Calendar cal=Calendar.getInstance();
		long millions=new Long(addtime).longValue()*1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");//HH-mm-ss
		return df.format(cal.getTime());
	}
}
